package com.example.springbootdemo.controller;

import com.example.springbootdemo.mapper.MessageMapper;
import com.example.springbootdemo.model.MessageInfo;
import com.example.springbootdemo.service.MessageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RequestMapping("/message")
@RestController
public class MessageController {
//    private List<MessageInfo> messagesInfos = new ArrayList<>();
    @Autowired
    private MessageService messageService;

    @RequestMapping("/publish")
    public Boolean publish(MessageInfo messageInfo) {
        if(!StringUtils.hasLength(messageInfo.getFrom()) || !StringUtils.hasLength(messageInfo.getTo()) || !StringUtils.hasLength(messageInfo.getMessage())) {
            return false;
        }
//        messagesInfos.add(messageInfo);
        messageService.addMessage(messageInfo);
        return true;
    }

    @RequestMapping("/getMessageInfo")
    public List<MessageInfo> getMessageInfo() {
//        return messagesInfos;
        return messageService.getMessageInfo();
    }
}
